const { getIPAddress } = require('../utils/getIp')
const ipAddress = getIPAddress()
class TrafficService {
  async login(params) {
    return {
      id: 1,
      token: 'exampleToken123',
      userInfo: {
        name: '建材王总AA',
        unitName: '亿达集团',
        phone: params.phone,
        avater: `http://${ipAddress}:2230/uploads/headImage.png`,
        isAuth: 0, // 0未认证，1已认证
        role: 'buildUnit', // police交警，buildUnit施工单位
      },
    }
  }

  // 获取标准化列表
  async getlist1() {
    const roads = ['道路一', '道路二', '道路三', '道路四', '道路五']
    const nodes = ['节点一', '节点二', '节点三', '节点四', '节点五']
    const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十']

    const generateData = () => {
      const list = []
      for (let i = 1; i <= 30; i++) {
        list.push({
          id: i,
          columnLabel1: `${roads[i % roads.length]}${'道路'.repeat((i % 5) + 1)}`,
          columnLabel2: `2025-${String(Math.floor(i / 5) + 1).padStart(2, '0')}-${String((i % 28) + 1).padStart(2, '0')} ${i % 24}:${i % 60}:${
            i % 60
          }`,
          columnLabel3: Math.floor(Math.random() * 3) + 1, // 随机返回1,2或3
          columnLabel4: `${nodes[i % nodes.length]}`,
          columnLabel5: Array.from({ length: (i % 4) + 1 }, (_, j) => names[(i + j) % names.length]),
        })
      }
      return list
    }

    return {
      list: generateData(),
      total: 30,
    }
  }
  async getlsitMy() {
    const roads = ['道路一', '道路二', '道路三', '道路四', '道路五']
    const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十']
    const orgs = ['组织1', '组织2', '组织3', '组织4', '组织5']
    const type = ['plan_audit', 'consult_opinion', 'sms_make', 'reach_info', 'reach_info_1', 'info_audit_2', 'plan_audit_3']
    const generateData = () => {
      const list = []
      for (let i = 1; i <= 30; i++) {
        list.push({
          id: i,
          columnLabel1: `${roads[i % roads.length]}${'道路'.repeat((i % 5) + 1)}`,
          columnLabel2: `2025-${String(Math.floor(i / 5) + 1).padStart(2, '0')}-${String((i % 28) + 1).padStart(2, '0')} ${i % 24}:${i % 60}:${
            i % 60
          }`,
          columnLabel3: names[i % names.length],
          columnLabel4: orgs[i % orgs.length],
          columnLabel5: `1${Math.floor(Math.random() * 9 + 1)}${Math.floor(Math.random() * 10000000000)
            .toString()
            .padStart(9, '0')}`,
          columnLabel6: (i % 5) + 1, // 按顺序循环返回1-5
          status: type[i % type.length],
        })
      }
      return list
    }

    return {
      list: generateData(),
      total: 30,
    }
  }
  async getRealTimeData() {
    return {
      data: {
        flyingDrones: {
          value: 42,
          trend: {
            value: 12,
            isPositive: true,
          },
          trendLabel: '同比昨日该时段均值',
          timeSeriesData: [
            {
              time: '00:00',
              timestamp: '2023-06-15T00:00:00.000Z',
              value: 12,
              isCurrent: false,
            },
          ],
        },
        alertsToday: {
          value: 7,
          trend: {
            value: 5,
            isPositive: false,
          },
          trendLabel: '同比昨日该时段均值',
          timeSeriesData: [],
        },
      },
      refreshInterval: 60000,
      timestamp: '2023-06-15T10:15:30.000Z',
    }
  }
  async getHistoryList() {
    return {
      total: 256,
      page: 1,
      pageSize: 10,
      records: [
        {
          id: 'F001',
          droneId: 'DJI-M300-001',
          takeoffTime: '2023-06-15 08:30:45',
          landingTime: '2023-06-15 09:45:12',
          duration: '1小时14分钟',
          durationMinutes: 74,
          location: '杭州市西湖区',
          coordinates: {
            lat: 30.2526,
            lng: 120.1652,
          },
          status: '已降落',
        },
      ],
    }
  }
  async getAlertList() {
    return {
      total: 256,
      page: 1,
      pageSize: 10,
      records: [
        {
          id: 'A001',
          timestamp: '2023-06-15T09:23:45',
          droneId: 'DJI-M300-001',
          location: '杭州市西湖区',
          coordinates: {
            lat: 30.2526,
            lng: 120.1652,
          },
          level: 'high',
          category: 'nofly',
          description: '无人机闯入禁飞区',
          zoneName: '西湖景区禁飞区',
          handled: true,
          handledTime: '2023-06-15T09:30:12',
          handledBy: '操作员A',
        },
      ],
    }
  }
  async getPointsData() {
    return {
      drones: [
        {
          id: 'drone-1',
          lat: 39.9142,
          lng: 116.4074,
          status: 'flying',
        },
        {
          id: 'drone-2',
          lat: 39.9242,
          lng: 116.4174,
          status: 'flying',
        },
      ],
      timestamp: '2023-06-15T10:15:30.000Z',
    }
  }
  async getPannelData() {
    return {
      alerts: [
        {
          id: '1',
          timestamp: '2023-06-15T09:23:45',
          location: '北京市海淀区中关村',
          level: 'high',
          description: '无人机闯入禁飞区',
          category: 'nofly',
          isNew: true,
          lat: 39.9242,
          lng: 116.4174,
          status: 'flying',
        },
      ],
      title: '实时活动看板',
      timestamp: '2023-06-15T10:15:30.000Z',
      refreshInterval: 20000,
    }
  }

  // 用户管理相关方法
  async getUserList(params = {}) {
    const { page = 1, pageSize = 10, username = '', status = '' } = params

    const departments = ['技术部', '市场部', '行政部', '财务部', '人力资源部']
    const roles = ['管理员', '普通用户', '访客', '审核员', '运维人员']
    const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十', '郑十一', '王十二']
    const organizations = ['总公司', '北京分公司', '上海分公司', '广州分公司', '深圳分公司']

    const generateUsers = () => {
      const users = []
      for (let i = 1; i <= 100; i++) {
        const nameIndex = (i - 1) % names.length
        const statusValue = i % 3 === 0 ? 0 : 1 // 0-禁用, 1-启用

        // 根据筛选条件过滤
        if ((username && !names[nameIndex].includes(username)) || (status !== '' && statusValue !== parseInt(status))) {
          continue
        }

        users.push({
          id: i,
          username: `user${i}`,
          realName: names[nameIndex],
          phone: `1${Math.floor(Math.random() * 9 + 1)}${Math.floor(Math.random() * 10000000000)
            .toString()
            .padStart(9, '0')}`,
          email: `user${i}@example.com`,
          department: departments[i % departments.length],
          organization: organizations[i % organizations.length],
          role: roles[i % roles.length],
          status: statusValue,
          createTime: `2023-${String((Math.floor(i / 10) % 12) + 1).padStart(2, '0')}-${String((i % 28) + 1).padStart(2, '0')} ${i % 24}:${i % 60}:${
            i % 60
          }`,
          lastLoginTime: `2023-${String((Math.floor(i / 8) % 12) + 1).padStart(2, '0')}-${String(((i + 5) % 28) + 1).padStart(2, '0')} ${
            (i + 2) % 24
          }:${(i + 15) % 60}:${(i + 30) % 60}`,
        })
      }

      // 根据筛选后的结果进行分页
      const filteredUsers = users.slice((page - 1) * pageSize, page * pageSize)

      return {
        list: filteredUsers,
        total: users.length,
      }
    }

    return generateUsers()
  }

  async getUserDetail(id) {
    const departments = ['技术部', '市场部', '行政部', '财务部', '人力资源部']
    const roles = ['管理员', '普通用户', '访客', '审核员', '运维人员']
    const names = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十']
    const organizations = ['总公司', '北京分公司', '上海分公司', '广州分公司', '深圳分公司']

    const userId = parseInt(id)
    const nameIndex = (userId - 1) % names.length

    return {
      id: userId,
      username: `user${userId}`,
      realName: names[nameIndex],
      phone: `1${Math.floor(Math.random() * 9 + 1)}${Math.floor(Math.random() * 10000000000)
        .toString()
        .padStart(9, '0')}`,
      email: `user${userId}@example.com`,
      department: departments[userId % departments.length],
      organization: organizations[userId % organizations.length],
      role: roles[userId % roles.length],
      status: userId % 3 === 0 ? 0 : 1,
      address: '杭州市西湖区',
      remark: `这是用户${userId}的备注信息`,
      createTime: `2023-${String((Math.floor(userId / 10) % 12) + 1).padStart(2, '0')}-${String((userId % 28) + 1).padStart(2, '0')} ${userId % 24}:${
        userId % 60
      }:${userId % 60}`,
      lastLoginTime: `2023-${String((Math.floor(userId / 8) % 12) + 1).padStart(2, '0')}-${String(((userId + 5) % 28) + 1).padStart(2, '0')} ${
        (userId + 2) % 24
      }:${(userId + 15) % 60}:${(userId + 30) % 60}`,
    }
  }

  async addUser(data) {
    // 模拟添加用户成功
    return {
      success: true,
      id: Math.floor(Math.random() * 1000) + 100,
    }
  }

  async updateUser(data) {
    // 模拟更新用户成功
    return {
      success: true,
      id: data.id,
    }
  }

  async updateUserStatus(id, status) {
    // 模拟更新用户状态成功
    return {
      success: true,
      id: parseInt(id),
      status: parseInt(status),
    }
  }

  async deleteUser(id) {
    // 模拟删除用户成功
    return {
      success: true,
      id: parseInt(id),
    }
  }

  async batchUpdateUserStatus(ids, status) {
    // 模拟批量更新用户状态成功
    return {
      success: true,
      ids,
      status: parseInt(status),
      affectedCount: ids.length,
    }
  }
}

module.exports = new TrafficService()
